//
//  ExhibitionCategoryViewController.m
//  HAODE
//
//  Created by LiYinDi on 16/4/8.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "ExhibitionCategoryViewController.h"
#import "ProductDetailViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "ExhibitionObject.h"
#import "ShareView.h"
#import "NewMediaPromotionViewController.h"
@interface ExhibitionCategoryViewController ()<TestDelegate,ShareViewDelegate,UIGestureRecognizerDelegate>

@end

@implementation ExhibitionCategoryViewController

{  ShareView*SV;
    UIView*shareblackview;
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [self setNavigation];
    [self setAutolayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setNavigation];
//    [self setAutolayout];
//    [_webView setPaginationMode:UIWebPaginationModeTopToBottom];
   [_webView setBackgroundColor:[UIColor clearColor]];
    NSURL *url = [[NSURL alloc]initWithString:_url];
//    NSLog(@"_url=====%@",_url);
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    // Do any additional setup after loading the view from its nib.
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
//    [self performSelector:@selector(getAchieveWebViewHeight) withObject:nil afterDelay:0.3];
    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    //js调用iOS
    //第一种情况
    //其中test1就是js的方法名称，赋给是一个block 里面是iOS代码
    
    ExhibitionObject *exhiObject=[ExhibitionObject  new];
    exhiObject.delegate=self;
    context[@"appJs"]=exhiObject;
    
//  
//    NSString *jsStr2=@"appJs.gotoItemDetail('参数1')";
//    [context evaluateScript:jsStr2];
}


-(void)CommonH5Page:(NSString *)title url:(NSString *)url shareType:(NSString *)shareType contentId:(NSString *)contentId{
    
    
    ProductDetailViewController *vc=[[ProductDetailViewController alloc] init];
    vc.url=url;
    vc.shareType=shareType;
//    vc.titleName=title;
    vc.itemID=contentId;
    [self.navigationController pushViewController:vc animated:YES  ];
    
}


-(void)productDetail:(NSString *)itemID Url:(NSString *)Url
{
    
//    NSLog(@"uuuuuuuuuuu");
//    ExhibitionPageItemViewController *vc=[[ExhibitionPageItemViewController alloc] init];
//    vc.url=url;
//    
//    [self.navigationController pushViewController:vc animated:YES  ];
    
    ProductDetailViewController *detailViewController = [[ProductDetailViewController alloc] init];
    detailViewController.itemID=itemID;
    detailViewController.url=Url;
    [self.navigationController pushViewController:detailViewController animated:YES];

}
-(void)getAchieveWebViewHeight
{
    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", _webView.frame.size.width];
    [_webView stringByEvaluatingJavaScriptFromString:meta];
}


-(void)setNavigation
{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
 
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0,100, 21)];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=self.titleName;
    
    self.navigationItem.titleView = titleLabel;
    
    self.navigationController.navigationBar.barStyle =UIBaselineAdjustmentNone ;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    
    UIImage* shareImage = [UIImage imageNamed:@"fenxiang"];
//    CGRect shareImageFrame = CGRectMake(0,0,30,30);
    UIButton* shareButton= [[UIButton alloc] initWithFrame:backframe];
    [shareButton setBackgroundImage:shareImage forState:UIControlStateNormal];
    shareButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [shareButton addTarget:self action:@selector(shareButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
}


-(void)closeKeyBoad
{
    [self.window makeKeyWindow];
    self.window.hidden=YES;
    
    
}
-(void)cancel:(NSString *)cancel{
    
    
    [self closeKeyBoad];
}
-(void)shareButtonAction
{
   
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.shareType forKey:@"contentType"];
    [param setValue:self.exhibitionID forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    NSLog(@"====%@",param);
    [HttpRequest requestData:@"Company/getShareUrl" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                [self.window makeKeyAndVisible];
                
                shareblackview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
                shareblackview.backgroundColor=[UIColor blackColor];
                shareblackview.alpha=0.65;
                [self.window addSubview:shareblackview];
                
                SV = [[ShareView alloc]initWithFrame:CGRectMake(0, ScreenHeight-245, ScreenWidth,245)];
                SV.dataDict=severDataDict.dataDict;
                SV.mDelegate=self;
                [self.window addSubview:SV];
                
                UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
                singleFingerOne.numberOfTouchesRequired = 1; //手指数
                singleFingerOne.numberOfTapsRequired = 1; //tap次数
                singleFingerOne.delegate = self;
                [shareblackview addGestureRecognizer:singleFingerOne];
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
                
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
}
-(void)successfullyReturns:(NSString *)successfullyReturns{//分享成功后回调登记接口
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSString* companyId = [user objectForKey:@"companyId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"scene" forKey:@"contentType"];
    [param setValue:self.exhibitionID forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [param setValue:companyId forKey:@"companyId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Company/ShareRecords/Add" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                [self closeKeyBoad];
                NewMediaPromotionViewController *loginVC=[[NewMediaPromotionViewController alloc] init];
                [self.navigationController pushViewController:loginVC animated:YES];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}
-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setAutolayout
{
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        
    }];
}


@end
